package com.itheima;

import java.util.ArrayList;
import java.util.Scanner;

/*
 * 学生管理类
 * */
public class StudentManager {
    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<>();

        boolean quit = true;

        while (quit) {
            System.out.println("---------欢迎使用学生管理系统---------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生信息");
            System.out.println("4 查询所有学生");
            System.out.println("5 退出系统");
            System.out.println("请输入你的选择：");

            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            switch (line) {
                case "1":
                    addStudent(students);
                    break;
                case "2":
                    deleteStudent(students);
                    break;
                case "3":
                    updateStudent(students);
                    break;
                case "4":
                    findAllStudent(students);
                    break;
                case "5":
                    quit = false;
                    System.out.println("谢谢使用");
                    break;
                default:
                    break;
            }
        }

    }

    // 添加学生信息
    public static void addStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学生学号：");
            sid = sc.nextLine();
            boolean flag = isUsed(arrayList, sid);
            if (flag) {
                System.out.println("你输入的学号已经被使用，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地：");
        String address = sc.nextLine();

        Student student = new Student();
        student.setSid(sid);
        student.setName(name);
        student.setAge(age);
        student.setAddress(address);

        boolean yes = arrayList.add(student);
        if (yes) {
            System.out.println("添加学生成功");
        }
    }

    // 判断学号是否被使用
    public static boolean isUsed(ArrayList<Student> arrayList, String sid) {
        boolean isUsed = false;

        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            if (s.getSid().equals(sid)) {
                isUsed = true;
                break;
            }
        }

        return isUsed;
    }

    // 查看学生信息
    public static void findAllStudent(ArrayList<Student> arrayList) {
        if (arrayList.size() == 0) {
            System.out.println("无信息，请先添加信息在查询。");
            return;
        }
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t地址");
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getAddress());
        }
    }

    // 删除学生信息
    public static void deleteStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号：");
        String sid = sc.nextLine();

        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getSid().equals(sid)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("该信息不存在，请重新输入");
        } else {
            arrayList.remove(index);
            System.out.println("删除学生成功");
        }
    }

    // 修改学生信息
    public static void updateStudent(ArrayList<Student> arrayList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        String sid = sc.nextLine();

        System.out.println("请输入学生新姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生新年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生新地址：");
        String address = sc.nextLine();

        Student s = new Student();
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getSid().equals(sid)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("该信息不存在，请重新输入");
        } else {
            arrayList.set(index, s);
            System.out.println("修改学生信息成功");
        }
    }
}
